N = 10000
itertimes = 50

def reverseNumber(n):
    s = str(n)
    s = list(s)
    s.reverse()
    return int(''.join(s))

def isPalidrome(n):
    s = list(str(n))
    if len(s) == 1:
        return True

    half = len(s) / 2 + 1
    for i in xrange(half):
        if s[i] != s[-i-1]:
            return False
    return True

isGood = {}
isLy = {}

for num in xrange(N-1, 0, -1):
    n = num
    for i in xrange(itertimes):
        m = reverseNumber(n)
        s = n + m
        if isPalidrome(s):
            isGood[num] = 1
            break
        if s in isGood:
            isGood[num] = 1
            break
        if s in isLy:
            isLy[num] = 1
            break
        n = s
    if not num in isGood:
        isLy[num] = 1

ans = 0
for i in xrange(1, N):
    if i in isLy:
        ans += 1

print ans




